package com.zhuiyun.project.security.customizelogin.wx;

import lombok.*;
import org.springframework.security.authentication.AbstractAuthenticationToken;
import org.springframework.security.core.GrantedAuthority;

import java.util.ArrayList;
import java.util.Collection;

/**
 * @ClassName WechatAuthenticationToken
 * @Description 身份验证令牌
 * @Author zcy
 * @Date 2023/4/5 15:00
 **/
@Getter
@Setter
@ToString
public class WechatAuthenticationToken extends AbstractAuthenticationToken {
    private static final long serialVersionUID = 530L;
    private String openId;
    private String userPhone;
    // 使用openid和userPhone创建一个未验证的token
    public WechatAuthenticationToken(String openId, String userPhone) {
        super((Collection)null);
        this.openId = openId;
        this.userPhone = userPhone;
        this.setAuthenticated(false);
    }

    // 使用openid和userPhone创建一个已验证的token
    public WechatAuthenticationToken(String openId, String userPhone, Collection<? extends GrantedAuthority> authorities) {
        super(authorities);
        this.openId = openId;
        this.userPhone = userPhone;
        super.setAuthenticated(true);
    }

    @Override
    public Object getCredentials() {
        return this.openId;
    }

    @Override
    public Object getPrincipal() {
        return this.userPhone;
    }

    public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException {
        if (isAuthenticated) {
            throw new IllegalArgumentException(
                    "Cannot set this token to trusted - use constructor which takes a GrantedAuthority list instead");
        }

        super.setAuthenticated(false);
    }

    @Override
    public void eraseCredentials() {
        super.eraseCredentials();
    }

}
